var roleCode = null;
var rcFlag = false;
var roleName = null;
var rnFlag = false;
var addBtn = null;


$(function () {
    roleCode = $("#roleCode");
    roleName = $("#roleName");
    addBtn = $("#add");
    roleCode.next().html("*");
    roleName.next().html("*");

    roleCode.bind("blur", function () {
        $.ajax({
            type: "POST",
            url: "/role/roleAdd",
            data: {method: "rcExist", roleCode: roleCode.val()},
            dataType: "json",
            success: function (data) {
                if (data.roleCode === "exist") {
                    roleCode.next().css({"color": "red"});
                    roleCode.next().html("* 该职位编码已存在");
                } else if (data.roleCode === "null") {
                    roleCode.next().css({"color": "orange"});
                    roleCode.next().html("* 职位编码不能为空");
                } else {
                    roleCode.next().css({"color": "green"});
                    roleCode.next().html("* √");
                    rcFlag = true;
                }
            },
            error: function (data) {
                roleCode.next().css({"color": "red"});
                roleCode.next().html("* 您输入的职位编码不正确");
            }
        });
    }).bind("focus", function () {
        roleCode.next().css({"color": "#666666"});
        roleCode.next().html("* 请输入职位编码");
    }).focus();

    roleName.bind("blur", function () {
        $.ajax({
            type: "POST",
            url: "/role/roleAdd",
            data: {method: "rnExist", roleName: roleName.val()},
            dataType: "json",
            success: function (data) {
                if (data.roleName === "exist") {
                    roleName.next().css({"color": "red"});
                    roleName.next().html("* 该职位名称已存在");
                } else if (data.roleName === "null") {
                    roleName.next().css({"color": "orange"});
                    roleName.next().html("* 职位名称不能为空");
                } else {
                    roleName.next().css({"color": "green"});
                    roleName.next().html("* √");
                    rnFlag = true;
                }
            },
            error: function (data) {

                roleName.next().css({"color": "red"});
                roleName.next().html("* 您输入的职位名称不正确");
            }
        });
    }).bind("focus", function () {
        roleName.next().css({"color": "#666666"});
        roleName.next().html("* 请输入职位名称");
    })


    addBtn.bind("click", function () {
        if (rcFlag && rnFlag) {
            if (confirm("是否确认提交数据")) {
                swal('添加成功！', '', 'success').then(function () {
                    $("#roleForm").submit();
                });
            }
        } else {
            swal("请填写正确信息！", "", "info");
        }
    });
});